﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports BusinessRule.IBusiness
Imports DatabaseAccess.IDAL
Imports System.Data
Imports BusinessRule.Common
Imports DatabaseAccess.DALFactory
Imports External.APICalling
Imports BusinessRule.DTO

Public Class uMESDomainManageBusiness
    Implements uMESDomainManageIBusiness
    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()

    ''' <summary>
    ''' 获取用户信息
    ''' </summary>
    ''' <returns></returns>
    Private Function GetEmployeeInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDomainManageIBusiness.GetEmployeeInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strEmployeename As String
            strEmployeename = ""
            If QueryCondition.Keys.Contains("Employeename") = True Then
                strEmployeename = QueryCondition("Employeename").ToString()
            End If

            strSQL = "SELECT Employee.employeeid, Employee.Employeename, Employee.MESPassword, Employee.Password,Employee.logintype, " & vbCrLf & _
                     "Employee.domainpasswordexpiredate,  factory.factoryid, factory.factoryname" & vbCrLf & _
                     "FROM Employee left join sessionvalues on employee.employeeid = sessionvalues.employeeid" & vbCrLf & _
                     "left join factory on factory.factoryid = sessionvalues.factoryid " & vbCrLf & _
                     "WHERE UPPER(Employee.Employeename) = UPPER('" & strEmployeename & "')" & vbCrLf & _
                     "ORDER BY Employee.Employeename" & vbCrLf

            Dim dtMain As DataTable = dal.GetCheckTempInfo(strSQL)

            oResult.DBTable = dtMain
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try

    End Function


    ''' <summary>
    ''' 修改域密码后修改MES用户密码
    ''' </summary>
    ''' <returns></returns>
    Private Function UpdateEmployeePwdByDomain(ByVal oInfo As CurrentUserInfo) As uMESProcessResult Implements uMESDomainManageIBusiness.UpdateEmployeePwdByDomain
        Dim oResult As New uMESProcessResult

        Try

            oResult = dal.UpdateEmployeePwdByDomain(oInfo)

            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try

    End Function

    ''' <summary>
    ''' 修改用户密码
    ''' </summary>
    ''' <param name="oUserInfo">修改用户密码的基础信息</param>
    ''' <param name="result">执行结果</param>
    ''' <param name="message">返回结果信息</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function UpdateUserPwd(ByVal oUserInfo As CurrentUserInfo, ByRef result As Boolean, ByRef message As String) As Boolean Implements uMESDomainManageIBusiness.UpdateUserPwd
        UpdateUserPwd = False
        result = False
        Try
            Dim strInfo As String = ""
            Dim success As Boolean = False
            Dim daServerHost As ServerHostInfo = oUserInfo.ServerHostInfo

            Dim strTxnDocName, strTxnName, strEventName As String
            strTxnDocName = "EmployeeMaintDoc"
            strTxnName = "EmployeeMaint"
            strEventName = "Load"

            Dim strUpdateName, strUpdateVersion, strUpdateCDOType As String
            strUpdateName = oUserInfo.EmployeeName
            strUpdateVersion = ""
            strUpdateCDOType = "NDO"

            Dim m_DataList As New List(Of ClientAPIEntity)

            Dim dataEntity As New ClientAPIEntity

            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Password"
            dataEntity.ClientDataValue = oUserInfo.Password
            m_DataList.Add(dataEntity)



            'Dim serverHost As String = daServerHost.ServerHost
            'Dim serverPort As String = daServerHost.ServerPort
            Dim serverUser As String = daServerHost.UserName
            Dim serverPassword As String = daServerHost.Password

            Dim clientData As New CamstarClientAPI(serverUser, serverPassword)
            success = clientData.UpdateModelByStandard(strTxnDocName, strTxnName, strUpdateName, strUpdateVersion, _
                                                            m_DataList, strInfo)
            '创建批次结束

            If success = True Then
                UpdateUserPwd = True
                result = True
            End If

            message = strInfo

        Catch ex As Exception
            result = False
            message = ex.Message
            Return False
        End Try
    End Function


    '修改本地用户密码
    'ligf
    Private Function UpdateEmployeeMESPassword(ByVal QueryCondition As Dictionary(Of String, String)) _
        As uMESProcessResult Implements uMESDomainManageIBusiness.UpdateEmployeeMESPassword

        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strEmployeename, strMESPassword As String
            strEmployeename = ""
            strMESPassword = ""

            If QueryCondition.Keys.Contains("Employeename") = True Then
                strEmployeename = QueryCondition("Employeename").ToString()
            End If

            If QueryCondition.Keys.Contains("MESPassword") = True Then
                strMESPassword = QueryCondition("MESPassword").ToString()
            End If

            strSQL = "update employee set employee.mespassword = '" & strMESPassword & "'" & vbCrLf & _
                    "WHERE UPPER(Employee.Employeename) = UPPER('" & strEmployeename & "')"

            Dim result As Integer = dal.UpdateUserPassword(strSQL)

            If result = 1 Then
                oResult.Result = True
                oResult.Message = "密码修改成功"

            End If

            Return oResult

        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult

        End Try
    End Function

    '验证本地用户密码
    'ligf
    Private Function CheckEmployeeMESPassword(ByVal QueryCondition As Dictionary(Of String, String)) _
  As uMESProcessResult Implements uMESDomainManageIBusiness.CheckEmployeeMESPassword

        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strEmployeename, strMESPassword As String
            strEmployeename = ""
            strMESPassword = ""

            If QueryCondition.Keys.Contains("Employeename") = True Then
                strEmployeename = QueryCondition("Employeename").ToString()
            End If

            If QueryCondition.Keys.Contains("MESPassword") = True Then
                strMESPassword = QueryCondition("MESPassword").ToString()
            End If



            strSQL = "SELECT Employee.Employeename, Employee.MESPassword, Employee.Password, Employee.logintype, Employee.domainpasswordexpiredate  " & vbCrLf & _
                    "FROM Employee " & vbCrLf & _
                    "WHERE UPPER(Employee.Employeename) = UPPER('" & strEmployeename & "')" & vbCrLf & _
                    "and  employee.mespassword = '" & strMESPassword & "'"

            Dim dt As DataTable = dal.CheckUserNameAndPassword(strSQL)

            oResult.DBTable = dt
            oResult.Result = True
            Return (oResult)

        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult

        End Try

    End Function

End Class
